package com.zwz.mall.api;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zwz.mall.managers.HttpRequestManager;
import com.zwz.mall.managers.UserManager;
import com.zwz.mall.utils.JSONUtils;
import com.zwz.mall.utils.ToastUtils;

import java.net.ConnectException;
import java.net.UnknownHostException;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * @Author: EzerXie
 * @Description:
 * @Date: Created on 2018/11/6 14:53
 * @Modified By:
 */

public abstract class BaseApi {
    public boolean mIsFirstLoaded = false;

    private boolean mIsLoading = false;

    protected BaseApiService getBaseApiService() {
        return HttpRequestManager.getInstance()
                .getApiService();
    }

    //第一次请求
    public boolean isFirstLoad() {
        return mIsFirstLoaded;
    }

    //等待上个请求完，才加载下次
    public boolean isLoading() {
        return mIsLoading;
    }

    /**
     * 创建具体条件和逻辑的请求
     * 回调
     *
     * @param requestListener
     */
    public abstract void loadData(OnRequestListener requestListener);

    /**
     * 解析响应结果
     * 处理数据
     *
     * @param jsonObject
     */
    public abstract void parseResult(JSONObject jsonObject);

    /**
     * 执行Retrofit请求（默认是异步请求）
     *
     * @param requestCall
     * @param requestListener
     */
    protected void loadData(final Observable<String> requestCall,
                            final OnRequestListener requestListener) {
        if (null == requestCall) {
            return;
        }

        mIsLoading = true;
        requestCall.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<String>() {

                    private String msg;

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        JSONObject jsonObject = JSON.parseObject(s);
                        String code = JSONUtils.getString ("ok", jsonObject);
                        msg = JSONUtils.getString("errorMsg", jsonObject);
                        if (code.equals ("true")){
                            //请求成功
                            parseResult(jsonObject);
                            mIsFirstLoaded = true;
                            requestListener.onSuccess();
                        }else {
                            //其他默认
                            requestListener.onFailure(null);
                            ToastUtils.showShort(msg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mIsLoading = false;
                        if (e instanceof UnknownHostException || e instanceof ConnectException) {
                            ToastUtils.showShort("网络错误");

                            if (null != requestListener) {
                                requestListener.onNetWorkError();//网络错误状态    缺省页的点击事件
                            }
                        } else {
                            if (null != requestListener) {
                                requestListener.onFailure(e);

                            }
                        }
                    }

                    @Override
                    public void onComplete() {
                        mIsLoading = false;
                    }

                });

    }


}
